local skel = fk.CreateSkill {
  name = "lb__ransheng",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable {
  ["lb__ransheng"] = "燃生",
  [":lb__ransheng"] = "<b>锁定技</b>，当你进入濒死状态时，你减少1点体力上限，摸一张牌并将体力值回复至1，然后你可将<a href=':lb__liushang'><font color='red'>〖流熵〗</font></a>中的一个已拥有技能与一个未拥有的技能互换；当你死亡时，你视为使用一张【杀】。",

  ["#lb__ransheng_death"] = "燃生",
  ["#lb__ransheng"] = "失去%arg，获得%arg2",

  ["$lb__ransheng1"] = "飞萤扑火，向死而生！",
  ["$lb__ransheng2"] = "为战而生，为生而战！",
  ["$lb__ransheng3"] = "直至一切，燃烧殆尽！",
}

skel:addEffect(fk.EnterDying, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.dying
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player, -1)
    if player.dead then return end
    player:drawCards(1, skel.name)
    room:recover {
      who = player,
      num = 1 - player.hp,
      recoverBy = player,
      skillName = skel.name
    }
    local x, y = {}, {}
    for _, s in ipairs({ "lb__hongyan", "lb__jiaotu", "lb__jianyuan" }) do
      if player:hasSkill(s, true) then
        table.insert(x, s)
      else
        table.insert(y, s)
      end
    end
    if #x == 0 or #y == 0 then return end
    local ask = {}
    local num = {}
    for _, n in ipairs(x) do
      for _, m in ipairs(y) do
        local t = "#lb__ransheng:::" .. n .. ":" .. m
        table.insert(ask, t)
        table.insert(num, { lose = n, acquire = m })
      end
    end
    table.insert(ask, "Cancel")
    local choices = room:askToChoice(player, {
      choices = ask,
      skill_name = skel.name,
    })
    if choices ~= "Cancel" then
      for i, v in ipairs(ask) do
        if choices == v then
          room:handleAddLoseSkills(player, "-" .. num[i].lose)
          room:handleAddLoseSkills(player, num[i].acquire)
        end
      end
    end
  end,
})

skel:addEffect(fk.Death, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name, false, true)
  end,
  on_cost = function(self, event, target, player, data)
    local use = player.room:askToUseVirtualCard(player, {
      name = "slash",
      skill_name = skel.name,
      cancelable = false,
      skip = true,
    })
    if use then
      event:setCostData(self, { use = use })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:useCard(event:getCostData(self).use)
  end,
})

return skel
